﻿
using System.Collections.Generic;
using System.Data;
using System.Text;
using Protocol.SouthGridProtocol.Blocks;
using Protocol.SouthGridProtocol.Decoder;
using Protocol.SouthGridProtocol.Encoder;
using PacoToolKit.Format;
namespace Protocol.SouthGridProtocol.Model
{
    public class C12H
    {
        private static string afn = "12";
        private static string head = "68";
        private static string bottom = "16";

        private static Seq seq = new Seq(0, 1, 1, 1, 8);
        private static C c = new C(0, 1, 1, null, 0, 11);//prm=1,funcode=11

        /// <summary>
        /// 生成下发读取任务数据的报文
        /// </summary>
        /// <param name="concentrator"></param>
        /// <param name="dataWithTimeDownLine"></param>
        /// <returns></returns>
        public static string encodeC12H(string concentrator, CDataWithTimeDownLine dataWithTimeDownLine)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";
            StringBuilder sb = new StringBuilder();
            sb.Append(dataWithTimeDownLine.da); sb.Append(dataWithTimeDownLine.di); sb.Append(dataWithTimeDownLine.beginTime); sb.Append(dataWithTimeDownLine.endTime);
            sb.Append(dataWithTimeDownLine.m);
            StringBuilder msgsb = new StringBuilder();
            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }

        /// <summary>
        /// 解析格式化任务日期
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string decodeFormatedMissionDate(string msg)
        {
            string date= msg.Substring(msg.Length - 16, 12);
            return string.Format("{0}-{1}-{2} {3}:{4}",
                date.Substring(0, 4),
                date.Substring(4, 2),
                date.Substring(6, 2),
                date.Substring(8, 2),
                date.Substring(10, 2));
        }

        /// <summary>
        /// 计算di长度
        /// </summary>
        /// <param name="diValue"></param>
        /// <returns></returns>
        public static int CalcMissionDataLength(string diValue)
        {
            int ret = 0;
            ret = C0CH.GetDataLength(diValue);
            if (ret > 0)
            {
                return ret;
            }
            ret = C0DH.getDataLength(diValue);
            if (ret > 0)
            {
                return ret;
            }
            return ret;
        }

        /// <summary>
        /// 解析上报数据
        /// </summary>
        /// <param name="msg">报文</param>
        /// <param name="diCount">di总数</param>
        /// <returns></returns>
        public static List<CData> decodeC12H(string msg,int diCount)
        {
            List<CData> list = new List<CData>();
            string data = string.Empty;
            string di = string.Empty;
            string da = msg.Substring(46, 4);
            string dataRemain = msg.Substring(50, msg.Length - 50 - 16);//初始 剩余未读数据
            //循环读取块
            int diRead = 0;
            while (dataRemain.Length > 8)
            {
                if (diRead < diCount)
                {
                    di =dataRemain.Substring(0, 8);
                    int dataLength = CalcMissionDataLength(Formater.ReverseHexString(di));
                    data = dataRemain.Substring(8, dataLength * 2);
                    dataRemain = dataRemain.Remove(0, 8 + (dataLength * 2));//重算长度
                    diRead++;
                    list.Add(new CData { da = da, di = di, data = data });
                }
                else
                {
                    //读到新测量点号码
                    da = dataRemain.Substring(0, 4);
                    dataRemain = dataRemain.Remove(0, 4);//重算长度
                    diRead = 0;//重置
                }
            }

            return list;
        }

        
    }
}
